<?php
declare (strict_types = 1);

namespace app\admin\controller;

class Setting extends BaseController
{

    public function getBase()
    {
        success(settings('site_name,site_logo,site_icon,site_beian,site_copyright,site_script'));
    }

    public function saveBase()
    {
        $data = $this->request->post(['site_name', 'site_logo', 'site_icon', 'site_beian', 'site_copyright', 'site_script']);
        $this->validate($data, [
            "site_name" => "max:20",
            "site_logo" => "max:255",
            "site_icon" => "max:20",
            "site_beian" => "max:20",
            "site_copyright" => "max:20",
            "site_script" => "max:255",
        ]);
        settings($data);
        success();
    }

    public function resetWechatApiToken()
    {
        settings('wechatapi_token', generate_wechatapi_token());
        success(settings('wechatapi_token'));
    }

    public function getWechat()
    {
        $result = settings('wechat_appid,wechat_appsecret,wechat_aeskey,wechatapi_token');
        mask_str($result['wechat_appsecret']);
        mask_str($result['wechat_aeskey']);
        if (empty($result['wechatapi_token'])) {
            settings('wechatapi_token', generate_wechatapi_token());
            $result['wechatapi_token'] = settings('wechatapi_token');
        }
        $result['wechatapi_url'] = (string) url('wechat/OfficialAccount/index')->suffix(false)->domain(true);
        $result['server_ip'] = config('app.server_ip');
        success($result);
    }

    public function saveWechat()
    {
        $data = $this->request->post(['wechat_appid', 'wechat_appsecret', 'wechat_aeskey']);
        $this->validate($data, [
            "wechat_appid" => "notEmpty|wechatAppId",
            "wechat_appsecret" => "notEmpty|wechatAppSecret",
            "wechat_aeskey" => "max:50",
        ]);
        settings($data);
        success();
    }

    public function getWechatPay()
    {
        $result = settings('wechatpay_mchid,wechatpay_mchkey,wechatpay_mchcert');
        mask_str($result['wechatpay_mchkey']);
        success($result);
    }

    public function saveWechatPay()
    {
        $data = $this->request->post(['wechatpay_mchid', 'wechatpay_mchkey', 'wechatpay_mchcert']);
        $this->validate($data, [
            "wechatpay_mchid" => "notEmpty|number|length:10",
            "wechatpay_mchkey" => "notEmpty|max:50",
            "wechatpay_mchcert" => "max:50",
        ]);
        settings($data);
        success();
    }

    public function getMiniProgram()
    {
        $result = settings('miniprogram_appid,miniprogram_appsecret');
        mask_str($result['miniprogram_appsecret']);
        success($result);
    }

    public function saveMiniProgram()
    {
        $data = $this->request->post(['miniprogram_appid', 'miniprogram_appsecret']);
        $this->validate($data, [
            "miniprogram_appid" => "notEmpty|wechatAppId",
            "miniprogram_appsecret" => "notEmpty|wechatAppSecret",
        ]);
        settings($data);
        success();
    }
}
